const { app, BrowserWindow, ipcMain, nativeTheme } = require('electron')
const path = require('path')

let win

app.whenReady().then(() => {
  win = new BrowserWindow({
    width: 600,
    height: 400,
    webPreferences: {
      contextIsolation: true,
      preload: path.join(__dirname, '../preload/index.js'),
      sandbox: false,
    },
  })
  win.loadFile(path.join(__dirname, '../renderer/index.html'))
  win.webContents.openDevTools({ mode: 'detach' })

  ipcMain.handle('isDarkMode', (event, args) => {
    return nativeTheme.shouldUseDarkColors
  })

  ipcMain.handle('setTheme', (event, theme) => {
    nativeTheme.themeSource = theme
    win.webContents.send('themeChanged', theme)
  })
})
